﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class shiti_update : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["sTittle"] = "试题修改";
            txtid.Text = Request.QueryString["zid"];
            txtid_bak.Text = txtid.Text;
            Conn zconn = new Conn();
            string zsql = "select que_subject,que_type,optionA,optionB,optionC,optionD,que_answer,note,score,que_lessonid from tb_questions where id=" + txtid.Text.Trim();
            SqlDataReader reader1= zconn.Reader(zsql);
            if (reader1.Read())
            {
                txtExamTitle.Text = reader1.GetString(0);
                string znoth1 = reader1.GetString(1);   //试题类型（que_type）--对应ddlType
                string znoth6="";   //答案---“问答题”类型没有答案，
                if (znoth1 != "问答题")
                {
                    txtExamA.Text = reader1.GetString(2);
                    txtExamB.Text = reader1.GetString(3);
                    txtExamC.Text = reader1.GetString(4);
                    txtExamD.Text = reader1.GetString(5);
                    znoth6 = reader1.GetString(6);   //答案（que_answer）,对应ddlAnswer,或者cblAnswer
                }
                
                txtInstruction.Text = reader1.GetString(7);
                txtscore.Text = reader1.GetInt32(8).ToString();
                txtid.Text = reader1.GetInt64(9).ToString();    //现在,TXTID.TEXT表示课程ID,而不是考试题目ID
                if (znoth1 == "单选题")
                {
                    ddlType.SelectedIndex=0;
                    if (znoth6 == "A")
                    {
                        ddlAnswer.SelectedIndex = 0;
                    }
                    if (znoth6 == "B")
                    {
                        ddlAnswer.SelectedIndex = 1;
                    }
                    if (znoth6 == "C")
                    {
                        ddlAnswer.SelectedIndex = 2;
                    }
                    if (znoth6 == "D")
                    {
                        ddlAnswer.SelectedIndex = 3;
                    }
                }
                if (znoth1 == "多选题")
                {
                    ddlType.SelectedIndex=1;
                    if (znoth6.IndexOf("A")>0)
                    {
                        cblAnswer.Items[0].Selected = true;
                    }
                    if (znoth6.IndexOf("B") > 0)
                    {
                        cblAnswer.Items[1].Selected = true;
                    }
                    if (znoth6.IndexOf("C") > 0)
                    {
                        cblAnswer.Items[2].Selected = true;
                    }
                    if (znoth6.IndexOf("D") > 0)
                    {
                        cblAnswer.Items[3].Selected = true;
                    }

                }
                if (znoth1 == "问答题")
                {
                    ddlType.SelectedIndex = 2;
                }

            }
            reader1.Dispose();
            zconn.Dispose();
            
            
        }
        if (ddlType.SelectedIndex == 0)
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
        }
        else
        {
            if (ddlType.SelectedIndex == 1)
            {
                Panel1.Visible = false;
                Panel2.Visible = true;
            }
            else
            {
                Panel1.Visible = false;
                Panel2.Visible = false;
            }
        }
        

    }

    
   
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string zanswer="";
        
        if (ddlType.Text == "单选题")
        {
            zanswer = ddlAnswer.Text;
        }
        if (ddlType.Text == "多选题")
        {
            zanswer = "";
            if (cblAnswer.Items[0].Selected == true)
            {
                zanswer = "A,";
            }

            if (cblAnswer.Items[1].Selected == true)
            {
                zanswer = zanswer + "B,";
            }
            if (cblAnswer.Items[2].Selected == true)
            {
                zanswer = zanswer + "C,";
            }
            if (cblAnswer.Items[3].Selected == true)
            {
                zanswer = zanswer + "D,";
            }
        }
        string zsql = "update tb_questions set que_subject='" + txtExamTitle.Text + "',que_type='" + ddlType.Text + "',optionA='" + txtExamA.Text + "',optionB='" + txtExamB.Text + "',optionC='" + txtExamC.Text + "',optionD='" + txtExamD.Text + "',que_answer='" + zanswer + "',note='" + txtInstruction.Text + "',score=" + txtscore.Text + ",xtsj2='" + DateTime.Now.ToString() + "' where id=" + txtid_bak.Text.Trim();
        
        Conn zconn = new Conn();
        int noth = zconn.ExeSql(zsql);
        if (noth == 1)
        {
            Response.Write("<script language=javascript>alert('操作正确,修改试题成功！！');if(opener!=null){opener.location.reload();window.close();}</script>");
        }
        else
        {
            Response.Write("<script language=javascript>alert('修改试题出错,请联络系统管理员！！');if(opener!=null){opener.location.reload();window.close();}</script>");
        }
        zconn.Dispose();

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //Page.Response.Redirect("shiti_update.aspx?zid=" + txtid.Text.Trim() );
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        //Response.Write("<script lanuage=javascript>window.close();location='javascript:history.go(-1)'</script>");//因在按钮事件触发前，已刷新一次页面，所以应是-2。
        Response.Write("<script language=javascript>history.go(-2);</script>");
    }
}
